package com.yc.boot.sys.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.boot.common.model.response.Outcome;
import com.yc.boot.log.aspect.WorkLog;
import com.yc.boot.sys.entity.SysDepartment;
import com.yc.boot.sys.business.SysDepartmentBusiness;
import com.yc.boot.sys.model.find.FindDeptPage;
import com.yc.boot.sys.model.find.FindDeptTree;
import com.yc.boot.sys.model.save.SaveDept;
import com.yc.boot.sys.model.update.ResetUserDept;
import com.yc.boot.sys.model.update.UpdateDept;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Set;

/**
 * @author 杨智杰
 * @since 2021/8/23 14:33
 */
@Api(tags = "部门相关接口")
@RestController
@RequestMapping("/sys/department")
public class SysDepartmentApi {

    @Resource
    private SysDepartmentBusiness sysDepartmentBusiness;

    @GetMapping
    @ApiOperation("分页")
    public Outcome<IPage<SysDepartment>> page(FindDeptPage param) {
        return Outcome.success(sysDepartmentBusiness.page(param));
    }

    @PostMapping
    @ApiOperation("新增")
    public Outcome save(@RequestBody @Valid SaveDept param) {
        return sysDepartmentBusiness.save(param);
    }

    @PutMapping
    @ApiOperation("修改")
    public Outcome update(@RequestBody @Valid UpdateDept param) {
        return sysDepartmentBusiness.update(param);
    }

    @DeleteMapping
    @ApiOperation("删除")
    public Outcome delete(@RequestBody @Valid @Size(min = 1) List<Long> id) {
        return Outcome.status(sysDepartmentBusiness.delete(id));
    }

    @GetMapping("/tree")
    @ApiOperation("查询部门树")
    public Outcome tree(FindDeptTree param) {
        List<Tree<String>> deptTree = sysDepartmentBusiness.getDeptTree(param);
        return CollUtil.isNotEmpty(deptTree)
                ? Outcome.success(deptTree)
                : Outcome.failure("未查询到相关数据!");
    }

    @PostMapping("/reset")
    @ApiOperation("重置用户部门关联")
    public Outcome reset(@RequestBody @Valid ResetUserDept param){
        return sysDepartmentBusiness.reset(param);
    }

    @GetMapping("/getDataScope")
    public Set<Long> getDataScope() {
        return sysDepartmentBusiness.getDataScope();
    }
}
